﻿using System;
using System.Xml;
using Autodesk.Revit.DB;

namespace YArchitech.HCLoad.Export
{
	internal class HcXmlSurface : HcXmlElement
	{
		public HcSurfaceData HcSurfaceData
		{
			get
			{
				return this.m_surfaceData;
			}
		}

		public HcXmlSurface(HcSurfaceData surfaceData)
		{
			this.m_surfaceData = surfaceData;
		}

		public override bool WriteElementData(ref XmlElement xmlelem_Surface)
		{
			bool result = base.WriteElementData(ref xmlelem_Surface);
			XmlDocument ownerDocument = xmlelem_Surface.OwnerDocument;
			xmlelem_Surface.SetAttribute("id", this.m_surfaceData.Id);
			xmlelem_Surface.SetAttribute("surfaceType", this.GetSurfaceTypeStr());
			xmlelem_Surface.SetAttribute("spaceIdRef", this.m_surfaceData.Space.Id.ToString());
			HcXmlElement.AppendTextElement(ref xmlelem_Surface, "Name", this.m_surfaceData.Name);
			if (this.m_surfaceData.AdjacentSpaceIds != null)
			{
				foreach (ElementId elementId in this.m_surfaceData.AdjacentSpaceIds)
				{
					XmlElement xmlElement = ownerDocument.CreateElement("AdjacentSpaceId");
					xmlElement.SetAttribute("spaceIdRef", elementId.ToString());
					xmlelem_Surface.AppendChild(xmlElement);
				}
			}
			this.AppendRectangularGeometryElement(ref xmlelem_Surface);
			this.AppendCadObjectIdElement(ref xmlelem_Surface);
			return result;
		}

		private string GetSurfaceTypeStr()
		{
			if (this.m_surfaceData.Exterior)
			{
				return "Exterior";
			}
			return "Interior";
		}

		private bool AppendRectangularGeometryElement(ref XmlElement xmlelem_Surface)
		{
			return false;
		}

		private bool AppendCadObjectIdElement(ref XmlElement xmlelem_Surface)
		{
			if (this.m_surfaceData.Host == null)
			{
				return false;
			}
			XmlDocument ownerDocument = xmlelem_Surface.OwnerDocument;
			Element host = this.m_surfaceData.Host;
			string sValue = "";
			string name = host.GetType().Name;
			if (!(name == "Floor"))
			{
				if (!(name == "Wall"))
				{
					if (name == "RoofBase")
					{
						sValue = (host as RoofBase).RoofType.Name;
					}
				}
				else
				{
					WallType wallType = (host as Wall).WallType;
					sValue = wallType.Kind.ToString() + ": " + wallType.Name;
				}
			}
			else
			{
				FloorType floorType = (host as Floor).FloorType;
				sValue = floorType.Category.Name + ": " + floorType.Name;
			}
			HcXmlElement.AppendTextElement(ref xmlelem_Surface, "CADObjectId", sValue);
			return true;
		}

		private HcSurfaceData m_surfaceData;
	}
}
